// pages/search/search.js
let {
  getHotdata,
  getSearchdata
} = require("../../api/search")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    findhot: [], //热门发现
    oneshow: true, //搜索历史发现
    twoshow: false, //没有商品
    threeshow: false, //商品列表
    // fourshow:false,  //综合分类
    keyword: '', //输入框输入值
    goodslists: null, //商品列表
    keylists: [], //历史记录
    hide: true, //搜索取消切换

  },

  // 获取输入搜索值
  getInputValue(e) { //e是event的简写，是一个事件
    this.setData({
      keyword: e.detail.value
    })
    console.log(e.detail)
  },


  Cancel() {
    this.setData({
      hide: true,
      oneshow: true,
      twoshow: false,
      threeshow: false
    })

  },




  // 搜索
  getSearch() {

    this.setData({
      hide: false
    })

    wx.showLoading({
      title: '加载中',
    })
    getSearchdata({
      query: this.data.keyword
    }).then(data => {
      console.log("内容", data)
      // let lists=data.data.list_v2
      // console.log("lists:",lists)

      if (!data) {
        this.data.oneshow = false
        this.data.twoshow = true
        this.data.threeshow = false
        // this.data.fourshow = true
        this.setData(this.data)
        return
      } else {
        this.data.oneshow = false
        this.data.twoshow = false
        this.data.threeshow = true
      }
      if (!data.data.list_v2.length) {
        this.data.oneshow = false
        this.data.twoshow = true
        this.data.threeshow = false
        this.setData(this.data)
        return
      }
      // if (data) {
      //   setTimeout(function () {
      // wx.hideLoading()
      //   }, 2000)
      // }

      let lists = data.data.list_v2
      this.data.goodslists = lists
      if (this.data.keyword != '') {
        let gunLsts = this.data.keylists.includes(this.data.keyword)
        if (!gunLsts) {
          this.data.keylists.push(this.data.keyword)
          wx.setStorageSync('history', this.data.keylists)
        }
      } else {
        this.data.oneshow = false
        this.data.twoshow = false
        this.data.threeshow = true
      }
      this.setData(this.data)
    })

    setTimeout(function () {
      wx.hideLoading()
    }, 2000)


  },


  // 搜索发现
  SearchFind(e) {

    wx.showLoading({
      title: '加载中',
    })

    this.setData({
      hide: false
    })

    let index = e.target.dataset.index;
    console.log(e)
    console.log("111", index)
    console.log(this.data.findhot[index].desc);

    getSearchdata({
      query: this.data.findhot[index].desc
    }).then(data => {
      console.log("hot数据", data)
      // let goods = data.data.list_v2
      // this.setData({
      //   goodslists:goods,
      //   iSshow:false,
      // })
      if (!data) {
        this.data.oneshow = false
        this.data.twoshow = true
        this.data.threeshow = false
        // this.data.fourshow = true
        this.setData(this.data)
        return
      } else {
        this.data.oneshow = false
        this.data.twoshow = false
        this.data.threeshow = true
      }
      if (!data.data.list_v2.length) {
        this.data.oneshow = false
        this.data.twoshow = true
        this.data.threeshow = false
        this.setData(this.data)
        return
      }
      if (data) {
        setTimeout(function () {
          wx.hideLoading()
        }, 2000)
      }

      let goods = data.data.list_v2
      this.data.goodslists = goods
      if (this.data.findhot[index].desc != "") {

        let gunLists = this.data.keylists.includes(this.data.findhot[index].desc)

        if (!gunLists) {
          this.data.keylists.push(this.data.findhot[index].desc)
        }

        wx.setStorageSync('history', this.data.keylists)
      } else {
        this.data.oneshow = false
        this.data.twoshow = false
        this.data.threeshow = true
      }
      this.setData(this.data)
    })

    // console.log("人们",this.data.findhot)
    // let index = this.data.findhot.findIndex((item=>{
    //   return item == i
    // }))
    // getSearchdata({query:index}).then(data=>{
    //   console.log("hot数据",data)
    // })
  },

  // 搜索历史
  gethist(e) {
    console.log(e)
    let index = e.target.dataset.index
    console.log(index)
    console.log("历史", this.data.keylists[index])

    getSearchdata({
      query: this.data.keylists[index]
    }).then((data) => {
      console.log("列表", data.data.list_v2)
      this.setData({
        goodslists: data.data.list_v2,
        oneshow: false,
        twoshow: false,
        threeshow: true,
        hide: false
      })
    })


  },


  // 清除历史记录
  del() {
    this.setData({
      keylists: []
    })
    wx.setStorageSync('history', this.data.keylists)
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    getHotdata().then(data => {
      console.log("热门：", data)
      let Hotlist = data.data.discover_list
      console.log("HOt", Hotlist)

      this.setData({
        findhot: Hotlist,
        keylists: wx.getStorageSync("history")
      })
    })


  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})